<!--
	作者：939868405@qq.com
	时间：2018-10-05
	描述：实物对账单-金额调整申请
	应付员测试帐号：102631 111111
	订单号：8100028053 8100028554
-->

<template>
  <div class="m-view-goodsAccountInfo m-window">
    <!--实物对账单浏览-->

    <el-row>
      <el-col :span="22">
        <el-row>
          <!--第一行、实物对账单号和申请日期-->
          <el-col :span="8">实物对账单号：{{ruleForm.gooAccNo}}</el-col>
          <el-col :span="14">申请时间：{{ruleForm.createDate}}</el-col>
        </el-row>
        <el-row>
          <!--第二行、公司和供应商-->
          <el-col :span="8">公司：{{ruleForm.companyName}}</el-col>
          <el-col :span="16">供应商：{{ruleForm.supplierName}}</el-col>
        </el-row>
        <el-row>
          <!--第三行、备注-->
          <el-col :span="24">备注：{{ruleForm.remark}}</el-col>
        </el-row>
      </el-col>
      <el-col :span="2">
        <el-button @click="cancel2">返回</el-button>
      </el-col>
    </el-row>

    <div class="m-mainContent m-top3">
      <!--表格、实物对账单汇总-->
      <div class="top">
        <div class="m-agTable">
          <ag-grid-vue
            class="m-agTableContent ag-theme-balham"
            :gridOptions="gD_total"
            :columnDefs="gD_total_columnDefs"
            :rowData="gD_total_rowData"
            v-loading="gD_total_loading"
            rowSelection="single"
          ></ag-grid-vue>
        </div>
      </div>
      <!--表格、实物对账单明细-->
      <div class="bottom">
        <div class="m-agTable">
          <ag-grid-vue
            class="m-agTableContent ag-theme-balham"
            :gridOptions="gD_info"
            :columnDefs="gD_info_columnDefs"
            :rowData="gD_info_rowData"
            v-loading="gD_info_loading"
            rowSelection="single"
          ></ag-grid-vue>
        </div>
      </div>
    </div>

    <!--新增差异调整-->
    <el-dialog
      class="m-recordTable"
      :title="Adjust_title"
      :visible.sync="Adjust_dialogVisible"
      v-if="Adjust_dialogVisible"
      width="60%"
      v-dialogDrag
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        :model="Adjust_ruleForm"
        ref="Adjust_ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        :rules="Adjust_rules"
        v-loading="diffLoading"
      >
        <!--第一行、订单号和行号-->
        <el-row>
          <el-col :span="12">
            <!--prop="purOrdNo"  检查输入内容时要用-->
            <!--placeholder="请输入10位的采购订单号，如：13702123456"-->
            <el-form-item label="订单号：">
              <el-input v-model="Adjust_ruleForm.purOrdNo" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行号：">
              <el-input v-model="Adjust_ruleForm.purOrdMxNo" :disabled="true"></el-input>
            </el-form-item>
          </el-col>

          <!--第二行、物料编号和物料描述-->
          <el-col :span="12">
            <el-form-item label="物料编号：">
              <el-input v-model="Adjust_ruleForm.matCode" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料描述：">
              <el-input v-model="Adjust_ruleForm.matName" :disabled="true"></el-input>
            </el-form-item>
          </el-col>

          <!--第三行、订单数量和单位-->
          <el-col :span="12">
            <el-form-item label="订单数量：">
              <el-input v-model="Adjust_ruleForm.orderAmount" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位：">
              <el-input v-model="Adjust_ruleForm.unitCode" :disabled="true"></el-input>
            </el-form-item>
          </el-col>

          <!--第三行、价格数量和价格单位-->
          <el-col :span="12">
            <el-form-item label="价格数量：">
              <el-input v-model="Adjust_ruleForm.priceAmount" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格单位：">
              <el-input v-model="Adjust_ruleForm.priceUnit" :disabled="true"></el-input>
            </el-form-item>
          </el-col>

          <!--第四行、订单单价和订单金额-->
          <el-col :span="12">
            <el-form-item label="订单单价：">
              <el-input v-model="Adjust_ruleForm.unitPrice" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单金额：" prop="unitmoney">
              <el-input v-model="Adjust_ruleForm.unitmoney" :disabled="true"></el-input>
            </el-form-item>
          </el-col>

          <!--第五行、实际单价和实际金额-->
          <el-col :span="12">
            <el-form-item label="实际单价：" prop="realPrice">
              <el-input
                v-model="Adjust_ruleForm.realPrice"
                :disabled="mode=='edit'"
                @keyup.enter.native="getrealMoney"
                @blur="getrealMoney"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际金额：" prop="realMoney" :show-message="false">
              <el-input v-model="Adjust_ruleForm.realMoney" :disabled="true"></el-input>
            </el-form-item>
          </el-col>

          <!--第六行、差异金额-->
          <el-col :span="12">
            <el-form-item label="差异金额：" prop="diffMoney">
              <el-input v-model="Adjust_ruleForm.diffMoney" :disabled="true"></el-input>
            </el-form-item>
          </el-col>

          <!--第七、八行、备注-->

          <el-col :span="24">
            <el-form-item label="备注：" prop="remark">
              <el-input
                v-model="Adjust_ruleForm.remark"
                type="textarea"
                :rows="3"
                placeholder="(100字以内)"
                :disabled="mode=='edit'"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第九行上传附件 -->
        <el-form-item label="附件:">
          <upLoadfile
            :files="files"
            :config="fileConfig"
            @fileChange="fileChange"
            :showBtn="mode=='add'"
          ></upLoadfile>
        </el-form-item>
      </el-form>
      <!--底部、取消和保存按钮-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel" v-show="mode=='add'">取消</el-button>
        <el-button type="primary" :disabled="btn_save_dis" @click="saveData" v-show="mode=='add'">保存</el-button>
        <el-button
          type="primary"
          :disabled="btn_submit_dis"
          @click="btn_submit"
          v-show="mode=='add'"
        >提交</el-button>
      </span>
    </el-dialog>

    <!--差异说明-->
    <el-dialog
      custom-class="m-connetModel"
      title="差异说明"
      v-if="difInfoDialogVisible"
      :visible.sync="difInfoDialogVisible"
      width="50%"
      v-dialogDrag
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="difDialogClose"
    >
      <el-card class="box-card" style="margin-bottom: 10px;">
        <div
          v-for="(item,key) in difInfo"
          :key="key"
          class="text item"
          style="margin-bottom:20px;overflow: hidden;"
        >
          <p
            style="text-align:center;font-size:11px;color:gray;padding-bottom:5px;overflow: hidden;width:100%;"
          >{{item.createDate}}</p>
          <span
            v-if="item.visible"
            :style="item.isSupSend == 1 ? 'float:left; text-align:left;' : 'float:right; text-align:right;'"
            style="width:80%; display: block;"
          >
            <p style="font-size:12px">{{item.sender}}</p>
            <p
              style="font-size:12px; color:blue; text-decoration:underline; cursor:pointer;"
              @click="downLoadFile(item)"
            >{{item.attachmentName}}</p>
            <p style="font-size:12px;">{{item.content}}</p>
          </span>
        </div>
      </el-card>
      <el-form
        :model="difRuleForm"
        ref="difRuleForm"
        class="demo-ruleForm"
        :rules="difRules"
        v-if="sendShow"
      >
        <el-form-item prop="difDetail" style="margin-bottom:0;">
          <upLoadfile :config="fileConfig" @fileChange="diffFileChange" ref="diffUpload"></upLoadfile>
          <el-row :gutter="10">
            <el-col :span="21">
              <el-input
                v-model="difRuleForm.difDetail"
                type="textarea"
                :rows="3"
                placeholder="请输入备注(可编辑100字)"
                maxlength="100"
                resize="none"
              ></el-input>
            </el-col>
            <el-col :span="3">
              <el-button type="primary" @click="sendDifInfo">发送</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>


<script>
import _Api from "@/api";
import localeText from "@/i18n/agGridText";
import constant from "@/public/constant";
import Vue from "vue";
import upLoadfile from "@/components/upLoadFile";
import downLoadFileFunc from "@/public/downLoadFile"
import addOperation from "@/public/addOperation"
import gridOptions from 'lesso-common/public/gridOptions';

const details = Vue.extend({
  template: `<span>
    <el-button :style=" params.node.data.isMsExist == 1 ? 'color:red' : '' " type="text" @click="details" class="rendererClass" size="mini">差异详情</el-button>
    <el-button type="text" class="rendererClass" size="mini" @click="unit_price_Adjust" v-if="price_adjust_show">单价调整</el-button>
    </span>`,
  data: function () {
    return {
      price_adjust_show: true,
    }
  },
  methods: {
    unit_price_Adjust () {
      this.params.context.componentParent.showDifDialog(
        this.params.node.data
      );
    },
    details () {
      this.params.context.componentParent.showDifDialog2(
        this.params.node.data);//
    },
  }
});

export default {
  data () {
    var check_realPrice = (rule, value, callback) => {
      setTimeout(() => {
        if (value) {
          if (value == "") {
            callback(new Error("请输入实际单价！"));
          } else callback();
        } else {
          callback(new Error("请输入实际单价！"));
        }
      }, 100);
    };
    var check_realMoney = (rule, value, callback) => {
      setTimeout(() => {
        if (value) {
          if (value == "") {
            callback(new Error("请输入实际金额！"));
          } else callback();
        } else {
          callback(new Error("请输入实际金额！"));
        }
      }, 100);
    };
    return {
      //弹出框-浏览实物对账单
      ruleForm: [],
      title: "",
      gD_total: [],
      gD_total_loading: false,
      gD_total_rowData: [],
      gD_info: [],
      gD_info_loading: false,
      gD_info_rowData: [],

      //弹出框-新增差异调整
      Adjust_title: "",
      Adjust_ruleForm: {},
      Adjust_dialogVisible: false,
      mode: "",
      fileList: [], //上传文件
      upload_url: this.$uploadUrl,
      btn_upload_dis: false,
      upload_show: false,
      Adjust_rules: {
        //验证规则
        realPrice: [
          {
            required: true,
            validator: check_realPrice,
            trigger: "change"
          }
        ],
        realMoney: [
          {
            required: true,
            validator: check_realMoney,
            trigger: "change"
          }
        ],
        remark: [
          {
            min: 0,
            max: 200,
            triggerr: "change",
            message: "长度在200个字符"
          }
        ]
      },
      in_realPrice_dis: false,
      in_remark_dis: false,
      btn_save_dis: false,
      btn_submit_dis: false,

      //弹出框-差异说明
      difInfoDialogVisible: false,
      difInfo: [],
      difRuleForm: {
        difDetail: ""
      },
      difDetailInfo: {
        orgName: '',
        fileName: '',
        fileSize: ''
      },
      difRules: {
        difDetail: [{
          min: 0,
          max: 100,
          trigger: "change",
          message: "长度在100个字符"
        }]
      },
      sendShow: true,
      // 文件上传传参
      userInfo: {
        userName: constant.userName,
        userId: constant.userId,
        groupName: 'SRM'
      },
      //  上传组件相关配置  
      fileConfig: {
        limits: 1,
        accept: '.pdf,.doc,.docx,.xls,.xlsx,.txt',
        maxSize: 20971520,
        multiple: true
      },
      // 回显已上传文件
      files: [],
      diffLoading:false
    };
  },
  beforeMount () {
    //实物对账单汇总-表格属性
    this.gD_total = {
      context: {
        componentParent: this
      },
      rowSelection: "multiple",
      enableColResize: true,
      localeText: localeText,
      suppressRowClickSelection: true
    };
    //表格列
    this.gD_total_columnDefs = [
      {
        headerName: "订单号",
        field: "purOrdNo",
        width: 130,
        checkboxSelection: true,
        headerCheckboxSelection: true,
        headerCheckboxSelectionFilteredOnly: true
      },
      { headerName: "行号", field: "purOrdMxNo", width: 50 },
      { headerName: "物料号", field: "matCode", width: 100 },
      { headerName: "物料描述", field: "matName", width: 200, tooltipField: 'matName' },
      { headerName: "单价", field: "unitPrice", width: 100 },
      { headerName: "订单数量", field: "orderAmount", width: 100 },
      { headerName: "入库数量", field: "amount", width: 100 },
      { headerName: "对账金额", field: "money", width: 100 }
    ];

    //实物对账单明细-表格属性
    this.gD_info = Object.assign({
      context: {
        componentParent: this
      },
      rowSelection: "multiple",
      enableColResize: true,
      localeText: localeText,
      suppressRowClickSelection: true
    },gridOptions())
    //表格列
    this.gD_info_columnDefs = [
      { headerName: "订单号", field: "purOrdNo", width: 100 }, //,hide:true
      { headerName: "行号", field: "purOrdMxNo", width: 70 },
      { headerName: "入库单号", field: "instockNo", width: 100 },
      { headerName: "行号", field: "instockMxNo", width: 70 },
      { headerName: "物料号", field: "matCode", width: 100 },
      { headerName: "物料描述", field: "matName", width: 200, tooltipField: 'matName' },
      { headerName: "单价", field: "unitPrice", width: 100 },
      { headerName: "入库数量", field: "amount", width: 100 },
      { headerName: "入库金额", field: "realMoney", width: 100 },
      { headerName: "差异金额", field: "adjustMoney", width: 100 },
      //          { headerName: "对账金额", field: "money", width: 100 },
      //			{	headerName:"订单金额",field:"orderMoney",width:100},
      {
        headerName: "操作",
        field: "adjust",
        width: 150,
        pinned: 'right',
        cellRendererFramework: details
      }
    ];
  },
  methods: {
    // 上传文件变化
    fileChange (files) {
      this.fileList = files
      this.files = []
    },
    // 差异文件变化
    diffFileChange (files) {
      console.log(files)
      this.difDetailInfo = {
        orgName: files[0].name,
        fileName: files[0].url,
        fileSize: files[0].size
      }
    },
    //打开差异调整页面
    showDifDialog (params) {
      var obj = {
        eqParams: {},
        likeParams: {},
        geParams: {},
        leParams: {}
      };
      obj.likeParams.purOrdNo = params.purOrdNo; //订单号
      obj.eqParams.purOrdMxNo = params.purOrdMxNo; //行号
      this.Adjust_dialogVisible = true;
      var that = this;
      this.diffLoading = true
      _Api.getAdjustbill(obj).then(function (respone) {
        var data = respone.data;
        if (data.code == "1") {
          if (data.total > 0) {
              that.diffLoading=false
            //浏览差异调整
            that.mode = "edit";
            that.Adjust_title = "浏览";
            that.Adjust_ruleForm = data.rows[0]; //看看Adjust_ruleForm是否有id
            // that.Adjust_ruleForm.priceAmount = params.priceAmount
            // that.Adjust_ruleForm.priceUnit = params.priceUnit
            // that.Adjust_ruleForm.unitmoney = addOperation.accMul(params.unitPrice,params.priceAmount);
            // that.Adjust_ruleForm.realMoney = addOperation.accMul(data.rows[0].realPrice,params.priceAmount);
            // that.Adjust_ruleForm.diffMoney = addOperation.accSub(that.Adjust_ruleForm.realMoney,that.Adjust_ruleForm.unitmoney)
            if (data.rows[0].state == 0) {
              //保存
              if (that.Adjust_ruleForm.attachmentName == "")
                //附件名称
                that.fileList = [];
              else
                that.files = [
                  {
                    name: that.Adjust_ruleForm.attachmentName,
                    url: that.Adjust_ruleForm.url,
                    md5: that.Adjust_ruleForm.md5 || '123',
                    iconShow: false
                  }
                ];
              that.in_realPrice_dis = false;
              that.in_remark_dis = false;
              that.btn_save_dis = false;
              that.btn_submit_dis = false;
              that.btn_upload_dis = false;
              that.upload_show = true;
            } //提交、通过
            else {
              if (that.Adjust_ruleForm.attachmentName == "")
                //附件名称
                that.fileList = [];
              else
                that.files = [
                  {
                    name: that.Adjust_ruleForm.attachmentName,
                    url: that.Adjust_ruleForm.url,
                    md5: that.Adjust_ruleForm.md5 || '123',
                    iconShow: false
                  }
                ];
              that.in_realPrice_dis = true;
              that.in_remark_dis = true;
              that.btn_save_dis = true;
              that.btn_submit_dis = true;
              that.btn_upload_dis = true;
              that.upload_show = false;
            }
          } else {
            //新增差异调整
            that.mode = "add";
            that.Adjust_title = "新增";
            obj = {};
            let sapObj = {
              //getPurOrdMxOfSap
              purOrdNo: params.purOrdNo,
              purOrdMxNo: params.purOrdMxNo
            }
            _Api.getPurOrdMxOfSap(sapObj).then(function (respone) {
              var data = respone.data;
              that.diffLoading = false
              if (data.code == 1) {
                let sapPurOrdData = data.rows;
                obj.purOrdNo = sapPurOrdData.purOrdNo;
                obj.purOrdMxNo = sapPurOrdData.purOrdMxNo;
                obj.matCode = sapPurOrdData.matCode;
                obj.matName = sapPurOrdData.matName;
                obj.orderAmount = sapPurOrdData.orderAmount;
                obj.unitCode = sapPurOrdData.unitCode;
                obj.unitPrice = sapPurOrdData.netPrice;
                obj.unitmoney = sapPurOrdData.netMoney;
                obj.priceAmount = sapPurOrdData.priceAmount;
                obj.priceUnit = sapPurOrdData.priceUnit;
                that.Adjust_ruleForm = obj;
                that.fileList = [];
                that.in_realPrice_dis = false;
                that.in_remark_dis = false;
                that.btn_save_dis = false;
                that.btn_submit_dis = false;
                that.btn_upload_dis = false;
                that.upload_show = true;
              } else {
                that.Adjust_ruleForm = obj;
                that.fileList = [];
                that.in_realPrice_dis = false;
                that.in_remark_dis = false;
                that.btn_save_dis = false;
                that.btn_submit_dis = false;
                that.btn_upload_dis = false;
                that.upload_show = true;
                that.$alert('获取信息失败！', {
                  showClose: false,
                  confirmButton: "确定",
                  customClass: "alertFailure"
                });
              }
            })
            // obj.purOrdNo = params.purOrdNo;
            // obj.purOrdMxNo = params.purOrdMxNo;
            // obj.matCode = params.matCode;
            // obj.matName = params.matName;
            // obj.orderAmount = params.orderAmount;
            // obj.unitCode = params.unitCode;
            // obj.unitPrice = params.unitPrice;
            // obj.unitmoney = addOperation.accMul(params.unitPrice,params.priceAmount); //params.orderMoney
            // obj.priceAmount = params.priceAmount
            // obj.priceUnit = params.priceUnit
            // that.Adjust_ruleForm = obj;
            // that.fileList = [];
            // that.in_realPrice_dis = false;
            // that.in_remark_dis = false;
            // that.btn_save_dis = false;
            // that.btn_submit_dis = false;
            // that.btn_upload_dis = false;
            // that.upload_show = true;
          }
        } else {
          that.diffLoading = false
          that.$alert(data.info, {
            showClose: false,
            confirmButton: "确定",
            customClass: "alertFailure"
          });
        }
      });
    },
    //差异调整-计算
    getrealMoney () {
      console.log("获取实际金额和差异金额");
      //检查实际单价是否为空
      var that = this;
      if (
        this.Adjust_ruleForm.realPrice == "" ||
        !this.Adjust_ruleForm.realPrice
      ) {
        this.$refs["Adjust_ruleForm"].validate(valid => {
          if (valid == false) {
            return false;
          }
        });
      }
      if (this.Adjust_ruleForm.realPrice) {
        this.Adjust_ruleForm.realPrice = this.Adjust_ruleForm.realPrice.replace(/[^\d.]/g, '')
        let realMoney = addOperation.accMul(this.Adjust_ruleForm.realPrice, this.Adjust_ruleForm.priceAmount)
        that.$set(that.Adjust_ruleForm, "realMoney", realMoney);
        let diffMoney = addOperation.accSub(realMoney, this.Adjust_ruleForm.unitmoney);
        that.$set(that.Adjust_ruleForm, "diffMoney", diffMoney);
      }
    },
    //上传文件的七个事件
    //1、点击上传
    submitUpload () {
      this.$refs.upload.submit();
    },
    //2、移除文件
    handleRemove (file, fileList) {
      this.fileList = fileList;
    },
    //3、点击文件
    handlePreview (file) {
      this.downLoadFile(this.fileList[0]);
    },
    //4、超出附件数量
    handleExceed (file, fileList) {
      this.$message.warning(`最多可添加 1 个附件`);
    },
    //5、
    upload_change (file, fileList) {
      this.upload_url = this.$uploadUrl;
      this.filename = file.name;
      // this.fileList = fileList;
    },
    //6、上传文件成功
    uploadSuccess (response, file, fileList) {
      if (response.code == 1) {
        this.$message({
          message: "上传成功",
          type: "success"
        });
        //保存文件信息
        this.fileList = [{
          name: response.rows[0].orgName,
          url: response.rows[0].fileName
        }];
        this.difDetailInfo = response.rows[0];
      } else {
        this.$alert("请求失败", {
          showClose: false,
          confirmButton: "确定",
          customClass: "alertFailure"
        });
        this.$refs.upload.clearFiles();
      }
    },
    //7、判断文件格式上传是否正确
    beforeAvatarUpload (file) {
      var fileType = file.name.substring(file.name.lastIndexOf(".") + 1).toLowerCase();
      if (
        fileType != "txt" &&
        fileType != "pdf" &&
        fileType != "doc" &&
        fileType != "docx" &&
        fileType != "xls" &&
        fileType != "xlsx"
      ) {
        this.$message({
          message: "上传文件类型只支持.txt/.pdf/.doc/.docx/.xls/.xlsx！",
          type: "warning"
        });
        return false;
      }
      const size = file.size / 1024 / 1024 < 20;
      if (!size) {
        this.$message({
          message: "上传模板大小不能超过 20MB!",
          type: "warning"
        });
        return false;
      }
    },
    downLoadFile (item) {
      var group = ''
      if (item.url.indexOf('SRM') > -1) {
        group = 'SRM'
      } else if (item.url.indexOf('SIGN') > -1) {
        group = 'SIGN'
      } else if (item.url.indexOf('TEST') > -1) {
        group = 'TEST'
      } else if (item.url.indexOf('MES') > -1) {
        group = 'MES'
      }
      var obj = {
        viewPath: item.url,
        groupName: group,
        fileName: ''
      };
      downLoadFileFunc(obj, item.attachmentName)
      // var that = this;
      // _Api.downLoadFile(obj).then(function(response){
      //     var data = response.data;
      //     if(data.code == "1")
      //     {
      //         if(data.total > 0)
      //         {
      //             console.log(data.rows[0].fileName);
      //             that.downLoad(item.name,data.rows[0].fileName);//data.rows[0].filename
      //         } else {
      //             that.$alert(data.info, {
      //                     showClose: false,
      //                     confirmButton: "确定",
      //                     customClass: "alertFailure"
      //                 });
      //         }
      //     }
      // })
    },
    downLoad (filename, url) {
      var eleLink = document.createElement('a');
      eleLink.style.display = 'none';
      eleLink.setAttribute("href", url);   //文件下载地址
      eleLink.setAttribute("download", filename);  //download属性只能在同域实现
      // 触发点击
      document.body.appendChild(eleLink);
      eleLink.click();
      // 然后移除
      document.body.removeChild(eleLink);
    },
    //提交差异调整
    btn_submit () {
      if (this.mode == "add") {
        //新增页面
        this.$refs["Adjust_ruleForm"].validate(valid => {
          if (valid == false) {
            return false;
          }
          var that = this;
          var obj = {};
          obj.id = ""; //主键
          obj.purOrdNo = this.Adjust_ruleForm.purOrdNo; //采购订单号
          obj.purOrdMxNo = this.Adjust_ruleForm.purOrdMxNo; //采购订单行号
          obj.matCode = this.Adjust_ruleForm.matCode; //物料编码
          obj.matName = this.Adjust_ruleForm.matName; //物料描述
          obj.orderAmount = this.Adjust_ruleForm.orderAmount; //订单数量
          obj.unitCode = this.Adjust_ruleForm.unitCode; //单位
          obj.unitPrice = this.Adjust_ruleForm.unitPrice; //订单单价
          obj.unitmoney = this.Adjust_ruleForm.unitmoney; //订单金额
          obj.realPrice = this.Adjust_ruleForm.realPrice; //实际单价
          obj.realMoney = this.Adjust_ruleForm.realMoney; //实际金额
          obj.remark = this.Adjust_ruleForm.remark; //备注
          obj.companyCode = this.ruleForm.companyCode;//公司编码
          obj.supplierCode = this.ruleForm.supplierCode;//供应商编码
          //提交时添加价格单位_20190712-LXL
          obj.priceUnit = this.Adjust_ruleForm.priceUnit;//价格单位
          if (this.fileList == "" || this.fileList == null) {
            obj.attachmentName = "";
            obj.url = "";
          } else {
            obj.attachmentName = this.fileList[0].name;
            obj.url = this.fileList[0].url;
          }

          obj.state = 1; //新增页面   提交   单据状态为提交
          obj.delFlag = 0; //新增页面   提交   显示状态为启用

          obj.creator = this.$global.user.employeeName; //创建人
          obj.creatorId = this.$global.user.id; //创建人ID
          _Api.insertAdjustbill(obj)
            .then(function (response) {
              var data = response.data;
              if (data.code == "1") {
                that.$message({
                  message: "提交成功",
                  type: "success"
                });
                that.Adjust_dialogVisible = false;
              } else {
                that.$alert(data.info, {
                  showClose: false,
                  confirmButton: "确定",
                  customClass: "alertFailure"
                });
              }
            })
            .catch(() => {
              that.$alert("请求失败！", {
                showClose: false,
                confirmButton: "确定",
                customClass: "alertFailure"
              });
            });
        });
      } else {
        //编辑页面
        this.$refs["Adjust_ruleForm"].validate(valid => {
          if (valid == false) {
            return false;
          }
          var that = this;
          var obj = {};
          obj.id = this.Adjust_ruleForm.id; //主键
          obj.purOrdNo = this.Adjust_ruleForm.purOrdNo; //采购订单号
          obj.purOrdMxNo = this.Adjust_ruleForm.purOrdMxNo; //采购订单行号
          obj.matCode = this.Adjust_ruleForm.matCode; //物料编码
          obj.matName = this.Adjust_ruleForm.matName; //物料描述
          obj.orderAmount = this.Adjust_ruleForm.orderAmount; //订单数量
          obj.unitCode = this.Adjust_ruleForm.unitCode; //单位
          obj.unitPrice = this.Adjust_ruleForm.unitPrice; //订单单价
          obj.unitmoney = this.Adjust_ruleForm.unitmoney; //订单金额
          obj.realPrice = this.Adjust_ruleForm.realPrice; //实际单价
          obj.realMoney = this.Adjust_ruleForm.realMoney; //实际金额
          obj.remark = this.Adjust_ruleForm.remark; //备注
          //提交时添加价格单位_20190712-LXL
          obj.priceUnit = this.Adjust_ruleForm.priceUnit;//价格单位
          if (this.fileList == "" || this.fileList == null) {
            obj.attachmentName = "";
            obj.url = "";
          } else {
            obj.attachmentName = this.fileList[0].name;
            obj.url = this.fileList[0].url;
          }

          obj.state = 1; //编辑页面   提交  单据状态为提交
          obj.delFlag = 0; //编辑页面   提交   显示状态为启用

          obj.modifyerId = this.$global.user.id; //修改人ID
          obj.modifyer = this.$global.user.employeeName; //修改人

          _Api.editAdjustbill(obj)
            .then(function (response) {
              var data = response.data;
              if (data.code == "1") {
                that.Adjust_dialogVisible = false;
                that.$message({
                  message: "提交成功",
                  type: "success"
                });
              } else {
                that.$alert(data.info, {
                  showClose: false,
                  confirmButton: "确定",
                  customClass: "alertFailure"
                });
              }
            })
            .catch(() => {
              that.$alert("请求失败！", {
                showClose: false,
                confirmButton: "确定",
                customClass: "alertFailure"
              });
            });
        });
      }
    },
    //保存差异调整
    saveData () {
      if (this.mode == "add") {
        //新增页面,insert
        this.$refs["Adjust_ruleForm"].validate(valid => {
          if (valid == false) {
            return false;
          }
          var that = this;
          var obj = {};
          obj.id = ""; //主键
          obj.purOrdNo = this.Adjust_ruleForm.purOrdNo; //采购订单号
          obj.purOrdMxNo = this.Adjust_ruleForm.purOrdMxNo; //采购订单行号
          obj.matCode = this.Adjust_ruleForm.matCode; //物料编码
          obj.matName = this.Adjust_ruleForm.matName; //物料描述
          obj.orderAmount = this.Adjust_ruleForm.orderAmount; //订单数量
          obj.unitCode = this.Adjust_ruleForm.unitCode; //单位
          obj.unitPrice = this.Adjust_ruleForm.unitPrice; //订单单价
          obj.unitmoney = this.Adjust_ruleForm.unitmoney; //订单金额
          obj.realPrice = this.Adjust_ruleForm.realPrice; //实际单价
          obj.realMoney = this.Adjust_ruleForm.realMoney; //实际金额
          obj.remark = this.Adjust_ruleForm.remark; //备注
          obj.companyCode = this.ruleForm.companyCode;//公司编码
          obj.supplierCode = this.ruleForm.supplierCode;//供应商编码
          //保存时添加价格单位_20190712-LXL
          obj.priceUnit = this.Adjust_ruleForm.priceUnit;//价格单位
          if (this.fileList == "" || this.fileList == null) {
            obj.attachmentName = "";
            obj.url = "";
          } else {
            obj.attachmentName = this.fileList[0].name;
            obj.url = this.fileList[0].url;
          }

          obj.state = 0; //新增页面   保存   单据状态为新增
          obj.delFlag = 0; //新增页面   保存   显示状态为启用

          obj.creatorId = this.$global.user.id; //创建人ID
          obj.creator = this.$global.user.employeeName; //创建人

          _Api.insertAdjustbill(obj)
            .then(function (response) {
              var data = response.data;
              if (data.code == "1") {
                that.$message({
                  message: "保存成功",
                  type: "success"
                });
                that.Adjust_dialogVisible = false;
              } else {
                that.$alert(data.info, {
                  showClose: false,
                  confirmButton: "确定",
                  customClass: "alertFailure"
                });
              }
            })
            .catch(() => {
              that.$alert("请求失败！", {
                showClose: false,
                confirmButton: "确定",
                customClass: "alertFailure"
              });
            });
        });
      } else {
        //编辑页面
        this.$refs["Adjust_ruleForm"].validate(valid => {
          if (valid == false) {
            return false;
          }
          var that = this;
          var obj = {};
          obj.id = this.Adjust_ruleForm.id; //主键
          obj.purOrdNo = this.Adjust_ruleForm.purOrdNo; //采购订单号
          obj.purOrdMxNo = this.Adjust_ruleForm.purOrdMxNo; //采购订单行号
          obj.matCode = this.Adjust_ruleForm.matCode; //物料编码
          obj.matName = this.Adjust_ruleForm.matName; //物料描述
          obj.orderAmount = this.Adjust_ruleForm.orderAmount; //订单数量
          obj.unitCode = this.Adjust_ruleForm.unitCode; //单位
          obj.unitPrice = this.Adjust_ruleForm.unitPrice; //订单单价
          obj.unitmoney = this.Adjust_ruleForm.unitmoney; //订单金额
          obj.realPrice = this.Adjust_ruleForm.realPrice; //实际单价
          obj.realMoney = this.Adjust_ruleForm.realMoney; //实际金额
          obj.remark = this.Adjust_ruleForm.remark; //备注
          //保存时添加价格单位_20190712-LXL
          obj.priceUnit = this.Adjust_ruleForm.priceUnit;//价格单位
          if (this.fileList == "" || this.fileList == null) {
            obj.attachmentName = "";
            obj.url = "";
          } else {
            obj.attachmentName = this.fileList[0].name;
            obj.url = this.fileList[0].url;
          }

          obj.state = 0; //编辑页面   保存   单据状态为新增
          obj.delFlag = 0; //编辑页面   保存   显示状态为启用

          obj.modifyerId = this.$global.user.id; //修改人ID
          obj.modifyer = this.$global.user.employeeName; //修改人
          _Api.editAdjustbill(obj)
            .then(function (response) {
              var data = response.data;
              if (data.code == "1") {
                that.$message({
                  message: "保存成功",
                  type: "success"
                });
                that.Adjust_dialogVisible = false;
              } else {
                that.$alert(data.info, {
                  showClose: false,
                  confirmButton: "确定",
                  customClass: "alertFailure"
                });
              }
            })
            .catch(() => {
              that.$alert("请求失败！", {
                showClose: false,
                confirmButton: "确定",
                customClass: "alertFailure"
              });
            });
        });
      }
    },
    handleClose (done) {
      this.$nextTick(_ => {
      });
      this.fileList = []
      done();
    },
    cancel () {
      this.$nextTick(_ => {
      });
      this.fileList = []
      this.Adjust_dialogVisible = false;
    },
    cancel2: function () {
      this.$emit("cancelComponents", true);
    },
    //打开差异说明页面
    showDifDialog2 (params) {
      this.sendShow = true;
      if (!params.id) {
        //新增未保存数据
        if (this.difInfo.length > 0) {
          for (let i = 0; i < this.difInfo.length; i++) {
            if (this.difInfo[i].instockNo == params.instockNo && this.difInfo[i].instockMxNo == params.instockMxNo && this.difInfo[i].phyCode == this.phyCode)
              this.difInfo[i].visible = true;
            else {
              this.difInfo[i].visible = false;
            }
          }
        }
        this.mxId = 0;
      } else {
        this.mxId = params.id;
        //已经存在ID
        this.getClickDetailInfo = params;
        var that = this;
        let obj = {
          id: params.id
        };
        for (let i = this.difInfo.length - 1; i >= 0; i--) {
          if (this.difInfo[i].id != 0)
            this.difInfo.splice(i, 1);
        }
        _Api.getGoodsAccountMxmsg(obj).then(function (response) {
          let data = response.data;
          if (data.code == "1") {
            for (let i = 0; i < data.rows.ltbl_goods_accountmxmsg.length; i++) {
              var obj = {};
              obj.content = data.rows.ltbl_goods_accountmxmsg[i].content;
              obj.instockNo = params.instockNo;
              obj.instockMxNo = params.instockMxNo;
              obj.createDate = data.rows.ltbl_goods_accountmxmsg[i].createDate;
              obj.isSupSend = data.rows.ltbl_goods_accountmxmsg[i].isSupSend;
              obj.sender = data.rows.ltbl_goods_accountmxmsg[i].sender;
              obj.senderId = data.rows.ltbl_goods_accountmxmsg[i].senderId;
              for (let j = 0; j < data.rows.ltbl_goods_accountmxmsg[i].lTblGoodsAccountattachment.length; j++) {
                obj.creator = data.rows.ltbl_goods_accountmxmsg[i].lTblGoodsAccountattachment[j].creator;
                obj.attachmentName = data.rows.ltbl_goods_accountmxmsg[i].lTblGoodsAccountattachment[j].attachmentName;
                obj.url = data.rows.ltbl_goods_accountmxmsg[i].lTblGoodsAccountattachment[j].url;
              }
              if (obj.instockNo == params.instockNo && obj.instockMxNo == params.instockMxNo)
                obj.visible = true;
              else
                obj.visible = false;
              obj.id = data.rows.ltbl_goods_accountmxmsg[i].id;
              obj.gooAccMxMsgId = data.rows.ltbl_goods_accountmxmsg[i].id;
              obj.phyCode = that.phyCode;
              obj.isExist = true;
              that.difInfo.unshift(obj);
            }
            //去除重复行
            for (let i = 0; i < that.difInfo.length; i++) {
              for (let j = 0; j < that.difInfo.length; j++) {
                if (i != j) {
                  if (that.difInfo[i].instockNo == that.difInfo[j].instockNo && that.difInfo[i].instockMxNo == that.difInfo[j].instockMxNo) {
                    if (that.difInfo[j].id == 0) {
                      that.difInfo.splice(j, 1);
                    }
                  }
                }
              }
            }
            for (let i = 0; i < that.difInfo.length; i++) {
              if (that.difInfo[i].instockNo == params.instockNo && that.difInfo[i].instockMxNo == params.instockMxNo)
                that.difInfo[i].visible = true;
              else
                that.difInfo[i].visible = false;
            }

            //未确认状态
            if (that.goodsAccInfo.billState == 1) {
              if (params.confirmFlag == "1") {
                that.sendShow = false;
              } else {
                that.sendShow = true;
              }
            }
            //已确认状态
            else if (that.goodsAccInfo.billState == 2) {
              if (that.$global.user.userTypeId == "4") {
                that.sendShow = false;
              }
              else {
                if (params.confirmFlag == "1")//已确认
                {
                  that.sendShow = false;
                  that.$set(details, 'price_adjust_show', false);
                }
                else {
                  let count = 0;
                  that.$set(details, 'price_adjust_show', true);
                  for (let i = 0; i < that.difInfo.length; i++) {
                    if (that.difInfo[i].instockMxNo == params.instockMxNo && that.difInfo[i].instockNo == params.instockNo)
                      count++;
                  }
                }
              }
            }
            //新增状态
            else {
              that.sendShow = false;
            }
            that.difInfoDialogVisible = true;
          } else {
            that.$alert(data.info, {
              showClose: false,
              confirmButton: "确定",
              customClass: "alertFailure"
            });
            that.difInfo = [];
          }
        }).catch(() => {
          that.$alert("操作失败", {
            showClose: false,
            confirmButton: "确定",
            customClass: "alertFailure"
          });
          that.difInfo = [];
        })
      }
      this.fileList = []
      this.clickRowData = params;
      this.upload_url = this.$uploadUrl;
      //this.upload_url="http://172.16.100.45:49231/srm/upload/file";
    },
    difDialogClose (done) {
      var that = this;
      this.getClickDetailInfo = {};
      if (this.pageLayOut == "add") {
        that.$refs["difRuleForm"].resetFields();
        that.$refs.upload.clearFiles();
      } else if (this.pageLayOut == "edit") {
        if (this.$global.user.userTypeId == "4") {
          if (this.goodsAccInfo.billState == "1") // && this.$global.user.userTypeId == "4"
          {
            if (that.$refs.difRuleForm)
              that.$refs["difRuleForm"].resetFields();
            if (that.$refs.upload)
              that.$refs.upload.clearFiles();
          }
        } else {
          if (this.goodsAccInfo.billstate == "2") {
            that.$refs["difRuleForm"].resetFields();
            that.$refs.upload.clearFiles();
          }
        }

      }
      this.difInfo = []
      this.fileList = []
      done();
    },
    beforeRemove (file, fileList) {

    },
    change (file) {
      this.upload_url = this.$uploadUrl;
      this.filename = file.name
    },
    sendDifInfo () {
      if (this.difDetailInfo.orgName == '' && this.difRuleForm.difDetail == '') {
        this.$alert("请上传相关附件/输入差异说明！", {
          showClose: false,
          confirmButton: "确定",
          customClass: "alertPrompt"
        });
        return;
      }
      if (!this.getClickDetailInfo.id) {
        this.sendBtn = true;
        let that = this;
        _Api.getServerTime().then(function (response) {
          let data = response.data;
          if (data.code == "1") {
            let dt = new Date(data.timestamp);
            let obj = {};
            that.$refs['diffUpload'].clearFileList()
            obj.createDate = dt.Format("yyyy-MM-dd HH:mm:ss");
            obj.attachmentName = that.difDetailInfo.orgName;
            obj.content = that.difRuleForm.difDetail;
            obj.phyCode = that.phyCode;
            obj.instockNo = that.clickRowData.instockNo;
            obj.instockMxNo = that.clickRowData.instockMxNo;
            obj.visible = true;
            obj.url = that.difDetailInfo.fileName;
            if (that.$global.user.userTypeId == "4") {
              obj.creator = that.$global.user.employeeName;
              obj.creatorId = that.$global.user.id;
              obj.isSupSend = 1;
              obj.sender = that.supplierInfo[0].supplierName;
              obj.senderId = that.supplierInfo[0].id;
            }
            else {
              obj.creator = that.$global.user.employeeName;
              obj.creatorId = that.$global.user.id;
              obj.isSupSend = 2;
              obj.sender = that.comName;
              obj.senderId = that.ruleForm.companyCode;
            }
            obj.size = that.difDetailInfo.fileSize;
            obj.id = 0;
            obj.gooAccMxMsgId = 0;
            that.difInfo.push(obj);//unshift
            that.$refs["difRuleForm"].resetFields();
            that.difDetailInfo = {
              orgName: '',
              fileName: '',
              fileSize: ''
            };
          }
          that.sendBtn = false;
        }).catch(() => {
          that.$alert(data.info, {
            showClose: false,
            confirmButton: "确定",
            customClass: "alertFailure"
          });
          that.sendBtn = false;
        })
      } else {
        var obj = {
          content: this.difRuleForm.difDetail,
          creator: this.$global.user.employeeName,
          creatorId: this.$global.user.id,
          "gReadFlag": 0,
          lTblGoodsAccountattachment: [
            {
              attachmentName: this.difDetailInfo.orgName,
              creator: this.$global.user.employeeName,
              creatorId: this.$global.user.id,
              size: this.difDetailInfo.fileSize,
              url: this.difDetailInfo.fileName
            }
          ]
        };
        if (this.$global.user.userTypeId == "4") {
          obj.isSupSend = 1
          obj.creator = this.$global.user.employeeName;
          obj.creatorId = this.$global.user.id;
          obj.sender = this.supplierInfo[0].supplierName;
          obj.senderId = this.supplierInfo[0].id;
        } else {
          obj.isSupSend = 2
          obj.creator = this.$global.user.employeeName;
          obj.creatorId = this.$global.user.id;
          obj.sender = this.comName;
          obj.senderId = this.ruleForm.companyCode;
        }
        // if(this.pageLayOut == "edit"){
        if (this.getClickDetailInfo.id != "") {
          obj.gooAccMxId = this.getClickDetailInfo.id;
        } else {
          obj.gooAccMxId = 0;
        }
        // }
        var that = this;
        this.sendBtn = true;
        _Api.addDiffDetailInfo(obj).then(function (response) {
          let data = response.data;
          if (data.code == "1") {
            that.$message({
              message: "保存成功",
              type: "success"
            });
            let obj = {};
            that.$refs['diffUpload'].clearFileList()
            obj.attachmentName = that.difDetailInfo.orgName;
            obj.content = that.difRuleForm.difDetail;
            obj.phyCode = that.phyCode;
            obj.instockNo = that.clickRowData.instockNo;
            obj.instockMxNo = that.clickRowData.instockMxNo;
            obj.visible = true;
            obj.url = that.difDetailInfo.fileName;
            if (that.$global.user.userTypeId == "4") {
              obj.creator = that.$global.user.employeeName;
              obj.creatorId = that.$global.user.id;
              obj.sender = that.supplierInfo[0].supplierName;
              obj.senderId = that.supplierInfo[0].id;
              obj.isSupSend = 1;
            }
            else {
              obj.creator = that.$global.user.employeeName;
              obj.creatorId = that.$global.user.id;
              obj.isSupSend = 2;
              obj.sender = that.comName;
              obj.senderId = that.ruleForm.companyCode;
            }
            obj.size = that.difDetailInfo.fileSize;
            obj.gooAccMxMsgId = 0;
            obj.id = 0;
            obj.gooAccMxMsgId = 0;
            obj.isExist = true;//标识
            let date2 = new Date(data.timestamp);
            obj.createDate = date2.Format("yyyy-MM-dd HH:mm:ss");
            that.difInfo.push(obj);//unshift
            that.$refs["difRuleForm"].resetFields();
            // that.$refs.upload.clearFiles();
            that.difDetailInfo = {
              orgName: '',
              fileName: '',
              fileSize: ''
            };
            that.sendBtn = false;
          } else {
            that.$alert(data.info, {
              showClose: false,
              confirmButton: "确定",
              customClass: "alertFailure"
            });
            that.sendBtn = false;
          }
        }).catch((err) => {
          that.$alert(err.data.message, {
            showClose: false,
            confirmButton: "确定",
            customClass: "alertFailure"
          });
          that.sendBtn = false;
        })
      }
    },
  },
  mounted () {
    this.ruleForm = this.goodsAccInfo;
    this.comName = this.goodsAccInfo.companyName
    // console.log("shuju",this.goodsAccInfo)
    var that = this;
    var obj = {};
    obj.gooAccNo = this.ruleForm.gooAccNo;
    _Api.getGoodsDetails(obj).then(function (response) {
      var data = response.data;
      if (data.code == 1) {
        if (data.total >= 0) {
          that.gD_total.api.setRowData(data.rows.m_StorageTotal);
          that.gD_total_loading = false;

          that.gD_info.api.setRowData(data.rows.m_StorageDetail);
          that.gD_info_loading = false;
        }
      }
    });
  },
  props: ["goodsAccInfo"],
  components: {
    upLoadfile
  }
};
</script>
